/*
 * @Author: lyx 2858535420@qq.com
 * @Date: 2023-02-28 17:21:09
 * @LastEditors: lyx 2858535420@qq.com
 * @LastEditTime: 2023-04-27 10:43:20
 * @FilePath: /antd-vue2-template/src/store/modules/permission.js
 * @Description: 菜单权限
 */
/**
 * 向后端请求用户的菜单，动态生成路由
 */
import { constantRouterMap } from '@/config/router.config';
import { generatorDynamicRouter } from '@/router/generator-routers';

const permission = {
  namespaced: true,
  state: {
    routers: constantRouterMap,
    addRouters: []
  },
  mutations: {
    SET_ROUTERS: (state, routers) => {
      state.addRouters = routers;
      state.routers = constantRouterMap.concat(routers);
    }
  },
  actions: {
    generateRoutes({ commit }, { token }) {
      return new Promise((resolve, reject) => {
        generatorDynamicRouter(token).then(routers => {
          commit('SET_ROUTERS', routers);
          resolve();
        }).catch(e => {
          reject(e);
        });
      });
    }
  }
};

export default permission;
